package com.neuedu.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.po.HouseInfoPO;
import com.neuedu.po.UserinfoPO;
import com.neuedu.service.HouseService;
import com.neuedu.util.FileUploadUtil;
import com.neuedu.vo.SelectItemVO;

@Controller
public class HouseHandler {

	@Autowired
	private HouseService hs;
	
	@RequestMapping("/getRentType.do")
	@ResponseBody
	public List<SelectItemVO> getRentTypeList() throws Exception{
		return hs.getRentTypeList();
	}
	
	
	@RequestMapping("/getHouseType.do")
	@ResponseBody
	public List<SelectItemVO> getHouseTypeList() throws Exception{
		return hs.getHouseTypeList();
	}
	
	@RequestMapping("/getHouseEquip.do")
	@ResponseBody
	public List<SelectItemVO> getHouseEquipList(int type) throws Exception{
		return hs.getHouseEquipList(type);
	}
	
	
	@RequestMapping("/addHouseStep1.do")
	@ResponseBody
	public int addHouseStep1(Integer rentType, Integer houseType, HttpSession session) throws Exception{
		HouseInfoPO po = new HouseInfoPO();
		po.setRtid(rentType);
		po.setHtid(houseType);
		session.setAttribute("add_house", po);
		return 0;
	}
	
	@RequestMapping("/addHouseStep2.do")
	@ResponseBody
	public int addHouseStep2(Integer unum, Integer bednum, Integer roomnum, HttpSession session) throws Exception{
		HouseInfoPO po = (HouseInfoPO)session.getAttribute("add_house");
		po.setUnum(unum);
		po.setBednum(bednum);
		po.setRoomnum(roomnum);
		session.setAttribute("add_house", po);
		return 0;
	}
	
	@RequestMapping("/addHouseStep3.do")
	@ResponseBody
	public int addHouseStep3(Integer wcnum, Integer iswcp, HttpSession session) throws Exception{
		HouseInfoPO po = (HouseInfoPO)session.getAttribute("add_house");
		po.setWcnum(wcnum);
		po.setIswcp(iswcp);
		session.setAttribute("add_house", po);
		return 0;
	}
	
	@RequestMapping("/addHouseStep4.do")
	@ResponseBody
	public int addHouseStep4(Integer area, HttpSession session) throws Exception{
		HouseInfoPO po = (HouseInfoPO)session.getAttribute("add_house");
		po.setArea(area);
		session.setAttribute("add_house", po);
		return 0;
	}
	
	@RequestMapping("/addHouseStep5.do")
	@ResponseBody
	public int addHouseStep5(String adress, HttpSession session) throws Exception{
		HouseInfoPO po = (HouseInfoPO)session.getAttribute("add_house");
		po.setAdress(adress);
		session.setAttribute("add_house", po);
		return 0;
	}
	
	@RequestMapping("/addHouseStep6.do")
	@ResponseBody
	public int addHouseStep6(@RequestParam(value="c1[]")int[] c1, @RequestParam(value="c2[]")int[] c2, HttpSession session) throws Exception{
		List<Integer> eqlist = new ArrayList<Integer>();
		if(c1 != null) {
			for(int i : c1) {
				eqlist.add(i);
			}
		}
		if(c2 != null) {
			for(int i : c2) {
				eqlist.add(i);
			}
		}
		session.setAttribute("add_house_equip", eqlist);
		return 0;
	}
	
	@RequestMapping("/addHouseStep7.do")
	@ResponseBody
	public int addHouseStep7(@RequestParam(value="c1[]")int[] c1, HttpSession session) throws Exception{
		List<Integer> eqlist = (List<Integer>)session.getAttribute("add_house_equip");
		if(c1 != null) {
			for(int i : c1) {
				eqlist.add(i);
			}
		}
		session.setAttribute("add_house_equip", eqlist);
		return 0;
	}
	
	@RequestMapping("/addHouseStep8.do")
	@ResponseBody
	public int addHouseStep8(HttpServletRequest request, HttpSession session) throws Exception{
		List<String> list = FileUploadUtil.uploadFiles(request, "/img/house");
		if(list != null && list.size() > 0) {
			HouseInfoPO po = (HouseInfoPO)session.getAttribute("add_house");
			po.setPhoto("/img/house/"+list.get(0));
			return 1;
		}else {
			return 0;
		}
		
	}
	
	@RequestMapping("/addHouseStep9.do")
	@ResponseBody
	public int addHouseStep9(String hname, double price, HttpSession session) throws Exception{
		HouseInfoPO po = (HouseInfoPO)session.getAttribute("add_house");  
		po.setHousename(hname);
		po.setPrice(price);
		UserinfoPO upo = (UserinfoPO) session.getAttribute("online");
		po.setUserid(upo.getUserid());
		List<Integer> list = (List<Integer>) session.getAttribute("add_house_equip");
		
		hs.saveNewHouseInfo(po, list);

		return 0;
	}
	
}
